package com.bankLogisticsSys.controller;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.bankLogisticsSys.interceptor.ManageLoginInterceptor;
import com.bankLogisticsSys.model.Userinfo;
import com.bankLogisticsSys.service.ManageUserService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

@Before(ManageLoginInterceptor.class)
public class ManageIndexController extends Controller{
	ManageUserService userService = new ManageUserService();
	
	public void index(){
		int role = getSessionAttr("role");
		setAttr("role", role);
		render("/WEB-INF/content/index.html");
	}
	
	@Clear
	public void login(){
		render("/WEB-INF/content/manageLogin.html");
	}
	
	public void logout(){
		getSession().invalidate();
		render("/WEB-INF/content/manageLogin.html");
	}
	@Clear
	public void dologin(){
		Userinfo user = userService.getUserByEmployeeID(getPara("username"));
		JSONObject obj = new JSONObject();
		if(user!=null&&StringUtils.isNotBlank(user.getEmployeeID())){
			if(getPara("password").equals(user.getPassword())){
				setSessionAttr("username", user.getEmployeeID());
				setSessionAttr("name", user.getName());
				setSessionAttr("networkid", user.getNetworkID());
				setSessionAttr("bank", user.getBank());
				setSessionAttr("role", user.getRole());
				obj.put("result", 0);	
			}else{
				obj.put("result", 1);
			}
		}else{
			obj.put("result", 1);
		}
		renderText(obj.toJSONString());
	}
	
	public void modifyPassword(){
		render("/WEB-INF/content/manageModifyPassword.html");
	}
	@Clear
	public void domodifyPassword(){
		String newPassword = getPara("newPassword");
		String oldPassword = getPara("oldPassword");
		String username = getPara("username");
		System.out.println(oldPassword);
		Userinfo user = userService.getUserByNameAndPass(username, oldPassword);
		if(user!=null && user.getPassword().equals(oldPassword)){
			user.setPassword(newPassword);
			user.update();
			render("/WEB-INF/content/manageLogin.html");
		}else{
			setAttr("errMsg","原密码错误，请重新输入");
			render("/WEB-INF/content/manageModifyPassword.html");
		}
	}
}
